package com.china08.yxyapi.api.manage.sch.platform.website.article;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.entity.mg.sws.SwsArticle;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;
import com.china08.yxyapi.repository.mg.SwsArticleRepository;
import com.china08.yxyapi.util.HtmlCompressorUtil;

@CrossOrigin
@RestController
public class ArticleUpdController extends YxyApiBaseController {

    Logger Log = Logger.getLogger(ArticleUpdController.class);

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @Autowired
    SwsArticleRepository swsArticleRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/website/article/upd", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object precess(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel) {
        String id = reqModel.id;
        String title = reqModel.title;
        String author = reqModel.author;
        Date showDate = reqModel.showDate;
        String attachmentDownloadUrl = reqModel.attachmentDownloadUrl;
        String attachmentFileName = reqModel.attachmentFileName;
        String content = reqModel.content;
        String section1Id = reqModel.section1Id;
        String section2Id = reqModel.section2Id;
        String faceImg = reqModel.faceImg;

        String schoolId = getProfile(request, response).getSchoolId();
        String senderId = getProfile(request, response).getId();
        String senderName = getProfile(request, response).getUserNick();

        Assert.isTrue(StringUtils.isNotBlank(id), "修改资讯不能为空");
        Assert.isTrue(StringUtils.isNotBlank(title), "标题不能为空");
        Assert.isTrue(StringUtils.isNotBlank(section1Id), "一级栏目不能为空");
        Assert.isTrue(StringUtils.isNotBlank(content), "内容不能为空");

        // 获取详情中的第一张图片作为推荐图片
        Document document = Jsoup.parse(content);
        Elements elements = document.select("img");
        if (elements.size() > 0) {
            faceImg = elements.get(0).attr("src");
        }
        content = HtmlCompressorUtil.HtmlCompressor(content);

        // 判断是否有二级栏目
        List<SwsSection> swsSection2List = swsSectionRepository.findBySchoolIdAndPidOrderByOrderNumAsc(schoolId, section1Id);
        // 有二级栏目，并且没有选择二级栏目
        if (StringUtils.isBlank(section2Id) && swsSection2List.size() > 0) {
            Assert.isTrue(false, "二级栏目不能为空");
        }

        // 补充显示时间:时分秒
        if (null == showDate) {
            showDate = new Date(System.currentTimeMillis());
        } else {
            int HOUR = Calendar.getInstance().get(Calendar.HOUR);
            int MINUTE = Calendar.getInstance().get(Calendar.MINUTE);
            int SECOND = Calendar.getInstance().get(Calendar.SECOND);
            showDate.setHours(HOUR);
            showDate.setMinutes(MINUTE);
            showDate.setSeconds(SECOND);
        }

        // 获取当前资讯所属的栏目，是否需要审核： 需要审核，修改后，资讯为代审核，不需要审核，资讯直接通过
        SwsSection swsSection = null;
        // 二级栏目为空
        if (StringUtils.isBlank(section2Id)) {
            swsSection = swsSectionRepository.findById(section1Id);
        } else {
            swsSection = swsSectionRepository.findById(section2Id);
        }
        // 审核通过
        Integer auditStatus = 1;
        // 当前栏目需要审核
        if (swsSection != null && swsSection.getIsAudit()) {
            auditStatus = 0;
        }

        // 删除老数据
        SwsArticle swsArticle = swsArticleRepository.findById(id);
        swsArticle.setAuthor(author);
        swsArticle.setSection1Id(section1Id);
        swsArticle.setSection2Id(section2Id);
        swsArticle.setTitle(title);
        swsArticle.setContent(content);
        // swsArticle.setOwnerSchoolId(schoolId);
        // swsArticle.setSenderId(senderId);
        // swsArticle.setSenderName(senderName);
        swsArticle.setShowDate(showDate);
        swsArticle.setAuditStatus(auditStatus);
        swsArticle.setAttachmentFileName(attachmentFileName);
        swsArticle.setAttachmentDownloadUrl(attachmentDownloadUrl);
        swsArticle.setFaceImg(faceImg);
        swsArticleRepository.save(swsArticle);
        return true;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String id;
        public String title;
        public String section1Id;
        public String section2Id;
        public String author;
        public Date showDate;
        public String attachmentFileName;
        public String attachmentDownloadUrl;
        public String content;
        public String faceImg;
    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
